package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.SmsHomeNewProduct;
import com.macro.mall.service.SmsHomeNewProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 首页新品管理Controller
 * @author Administrator
 * @date 2020/5/31
 * @since
 */
@Controller
@Api(tags = "SmsHomeNewProductController", description = "首页新品管理")
@RequestMapping("/home/newProduct")
public class SmsHomeNewProductController {
	@Autowired
	private SmsHomeNewProductService homeNewProductService;

	@ApiOperation("添加首页推荐品牌")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult create(@RequestBody
		List<SmsHomeNewProduct> homeBrandList) {
		int count = homeNewProductService.create(homeBrandList);
		if (count > 0) {
			return CommonResult.success(count);
		}
		return CommonResult.failed();
	}

	@ApiOperation("修改推荐排序")
	@RequestMapping(value = "/update/sort/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateSort(@PathVariable
		Long id, Integer sort) {
		int count = homeNewProductService.updateSort(id, sort);
		if (count > 0) {
			return CommonResult.success(count);
		}
		return CommonResult.failed();
	}

	@ApiOperation("批量删除推荐")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult delete(@RequestParam("ids") List<Long> ids) {
		int count = homeNewProductService.delete(ids);
		if (count > 0) {
			return CommonResult.success(count);
		}
		return CommonResult.failed();
	}

	@ApiOperation("批量修改推荐状态")
	@RequestMapping(value = "/update/recommendStatus", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam Integer recommendStatus) {
		int count = homeNewProductService.updateRecommendStatus(ids, recommendStatus);
		if (count > 0) {
			return CommonResult.success(count);
		}
		return CommonResult.failed();
	}

	@ApiOperation("分页查询推荐")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult<CommonPage<SmsHomeNewProduct>> list(@RequestParam(value = "productName", required = false) String productName,
		@RequestParam(value = "recommendStatus", required = false) Integer recommendStatus,
		@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
		@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		List<SmsHomeNewProduct> homeBrandList = homeNewProductService.list(productName, recommendStatus, pageSize, pageNum);
		return CommonResult.success(CommonPage.restPage(homeBrandList));
	}
}
